/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;

/**
 *
 * @author Jeppe
 */
 public class Observer {

    private static Observer instance;
    private final ArrayList<ActionListener> listeners;

    private Observer() {
        listeners = new ArrayList<>();
    }

    public static Observer getInstance() {
        if (instance == null) {
            instance = new Observer();
        }
        return instance;
    }

    public void addListener(ActionListener actionListener) {
        if (!listeners.contains(actionListener)) {
            listeners.add(actionListener);
        }
    }

    public void notifyListeners(Object object, String str) {
        ActionEvent actionEvent = new ActionEvent(object, ActionEvent.ACTION_PERFORMED, str);

        for (int i = 0; i < listeners.size(); i++) {
            listeners.get(i).actionPerformed(actionEvent);
        }
    }

}
